<template>
  <view>
    <uni-search-bar bg-color="white" @confirm="searchPerson" @cancel="mycancel" placeholder="请输入姓名或身份证号码"></uni-search-bar>
    <view class="list-box">
      <view class="title">选择被发放人</view>
      <view class="list" v-for="(item,index) in showWorkerary" :key="index">
        <view class="left">
          <view class="img-box">
            <image src="../../../static/images/组 118.png"></image>
          </view>
          <view class="info-person">
            <view class="">{{item.personName}}</view>
            <view class="zi">{{item.personIdentityCard}}</view>
          </view>
        </view>
        <view class="righ">
          <label class="radio">
            <radio color="#2f67ff" :value="submitlist[index].index" :checked="submitlist[index].flag"
              @click="saveworkerinfo(index)" /><text></text>
          </label>
        </view>
      </view>
      <!-- <uni-data-checkbox :localdata="range" @change="change"></uni-data-checkbox> -->
      <!-- <uni-indexed-list :options="list" :show-select="true" @click="bindClick" /> -->
    </view>
    <!-- <uni-indexed-list :options="man"></uni-indexed-list> -->
    <view class="enter-btn" v-show="isNext">
      <button @click="putcontract">确认</button>
    </view>
  </view>
</template>

<script>
  import myutil from '../../../common/util.js'
  export default {
    data() {
      return {
        isNext: false, //是否选择了发送人 没有选择就不显示 确认
        userType: null,
        mie: {
          id: 0,
          personName: '',
          personIdentityCard: ''
        },
		disttype3:'',
        disttype2: '',
        disttype1: '',
        distlist: [],
        Workerindex: 0,
        projectName: '',
        projectId: '',
        workerary: [], //全部人员
        showWorkerary: [], // 展示的全部人员
        ischeck: [],
        submitlist: [],
		type:''
      }
    },
    onLoad(options) {
	  this.type=options.id
      this.userType = uni.getStorageSync('usertype')//获取当前登录角色身份
      this.selectework()
      this.getdist()
      console.log('userid', uni.getStorageSync('userId'));
      // this.mie.id=uni.getStorageSync('userId')
      // this.mie.personName=uni.getStorageSync('nickName')+'(本人)'
      // this.mie.personIdentityCard=uni.getStorageSync('userCode')
    },
    created() {
      this.userType = uni.getStorageSync('usertype')//获取当前登录角色身份
	  console.log('---usertype',this.userType);
    },
    methods: {
	  calculateAge(idNumber) { 
		  // 从身份证号码中提取出生日期，假设身份证号码中出生日期的格式为"YYYYMMDD"
		   const birthYear = parseInt(idNumber.substr(6, 4), 10); const birthMonth = parseInt(idNumber.substr(10, 2), 10);
			const birthDay = parseInt(idNumber.substr(12, 2), 10); 
			// 获取当前日期 
			const currentDate = new Date(); 
			const currentYear = currentDate.getFullYear(); 
			const currentMonth = currentDate.getMonth() + 1; 
			// 月份从0开始，所以要加1
			 const currentDay = currentDate.getDate();
			  // 计算年龄 
			  let age = currentYear - birthYear; 
			  // 根据出生月份和日期判断是否已经过生日 
			  if (currentMonth < birthMonth || (currentMonth === birthMonth && currentDay < birthDay)) { age--; }
			   return age; 
	  },
      // 点击了取消
      mycancel(){
        this.showWorkerary = this.workerary
      },
      searchPerson(e) {
        console.log(e.value);
        this.showWorkerary = []
        console.log('myutil.isSalaryValid(e.value)',myutil.isSalaryValid(e.value));
        if(myutil.isSalaryValid(e.value)){
          // 数字判断身份证
          this.workerary.forEach(item => {
            if(item.personIdentityCard.indexOf(e.value) != -1){
              this.showWorkerary.push(item)
            }
          })
        }else{
          this.workerary.forEach(item => {
            if(item.personName.indexOf(e.value) != -1){
              this.showWorkerary.push(item)
            }
          })
        }
      },
      getdist() {//获取查询字典所需值
        uni.$get(this.path + '/system/dict/type/list').then(res => {
          if (res.code == 200) {
            console.log('fangfazhixingg',res);
            this.distlist = res.rows
            for (let i = 0; i < this.distlist.length; i++) {
              if (this.distlist[i].dictName == '班组类型') {
                this.disttype1 = this.distlist[i].dictType
              }
              if (this.distlist[i].dictName == '单位类型') {
                this.disttype2 = this.distlist[i].dictType
              }
			  if(this.distlist[i].dictName=='工作岗位'){
				  this.disttype3=this.distlist[i].dictType
			  }
            }
            uni.setStorageSync('leixing', this.disttype1)
            uni.setStorageSync('danwei', this.disttype2)
			uni.setStorageSync('gangwei',this.disttype3)
			console.log('gangwei',this.disttype3);
          }
        });

        // uni.$get(this.path+'system/dict/data/type/'+this.disttype1).then(res=>{
        // 	if(res.code==200){
        // 		for(let i=0;i<res.data.length;i++){
        // 			this.Workertypelist.push(res.data[i].dictLabel)
        // 		}
        // 	}
        // })
      },
      tomadecontract() {//该方法跳转复杂合同填写页面
        uni.navigateTo({
          url: './newcontract'
        })
      },
      saveworkerinfo(e) {
        console.log(e);
        this.isNext = true // 只要点击了 选人就可以展示确认按钮
        this.submitlist[e].flag = true
        console.log(this.submitlist[e]);
        console.log('------suzhu', this.submitlist.length);
        for (let i = 0; i < this.submitlist.length; i++) {
          console.log('>>>>');
          this.submitlist[i].flag = false
          if (i == e) {
            this.submitlist[i].flag = true
          }
        }
      },
      bindClick(e) {
        console.log('点击item，返回数据' + JSON.stringify(e))
      },
      putcontract() {//跳转填写合同信息页面
        for (let i = 0; i < this.submitlist.length; i++) {
          if (this.submitlist[i].flag) {
            this.Workerindex = i
          }
        }
		console.log(this.workerary);
		console.log('年龄',this.calculateAge(this.workerary[this.Workerindex].personIdentityCard));
		if(this.calculateAge(this.workerary[this.Workerindex].personIdentityCard)>=60&&this.workerary[this.Workerindex].personSex==1){
			uni.showToast({
					title:'该人年龄过高，根据法律签约规定，签约类型为劳动协议签约',
					icon:'none',
					duration:1000
			})
			setTimeout(()=>{
			uni.navigateTo({ //personIdentity 0包工头 1劳工 其他为其他
			  url: './labourOrmigrant?id=' + this.workerary[this.Workerindex].id+'&type='+1+'&isheader='+this.workerary[this.Workerindex].personIdentity
			})
			},1000)
			
			return
		}
		if(this.calculateAge(this.workerary[this.Workerindex].personIdentityCard)>=50&&this.workerary[this.Workerindex].personSex==0){
			uni.showToast({
					title:'该人年龄过高，已自动跳转劳工协议',
					icon:'none',
					duration:1000
			})
			setTimeout(()=>{
			uni.navigateTo({
			  url: './labourOrmigrant?id=' + this.workerary[this.Workerindex].id+'&typem='+1+'&isheader='+this.workerary[this.Workerindex].personIdentity
			})
			},1000)
			
			return
		}
		if(this.type==1){
			uni.navigateTo({
			  url: './labourOrmigrant?id=' + this.workerary[this.Workerindex].id+'&isheader='+this.workerary[this.Workerindex].personIdentity
			})
			return
		}
        uni.navigateTo({
          url: './newcontract?id=' + this.workerary[this.Workerindex].id+'&isheader='+this.workerary[this.Workerindex].personIdentity
        })
      },
      
      
      getPersonList() {
        // 查询劳工
        console.log('查询劳工');
        uni.$post(this.path + '/person/query', {
          isHeader: 3, //此参数需动态判断-------------------------------》
          pageNo: 1,
          pageSize: 5000,
          personGroup: 10,
          personIdentity: 3,
          personStatus: 3,
          returnFields: ""
        }).then(res => {
          if (res.code == 200) {
            this.workerary = res.data.records
            this.showWorkerary = res.data.records
            console.log(this.workerary);
			for(let i=0;i<this.showWorkerary.length;i++){
				if(this.showWorkerary[i].personIdentityCard==uni.getStorageSync('userCode')){
					uni.setStorageSync('workerId',this.showWorkerary[i].id)//包工头id
					this.showWorkerary.splice(i,1)
					this.workerary.splice(i,1)
				}
			}
			console.log('filter of arry',this.showWorkerary);
            for (let i = 0; i < this.workerary.length; i++) {
				// if(this.userType==3&&uni.getStorageSync('userCode')==this.workerary[i].personIdentityCard){
				// 	break
				// }
				this.submitlist.push({
                flag: false,
                index: 0,
				})
            }
            console.log(this.submitlist);

          }
        })
      },

      //查询包工头
      getLabourList() {
        console.log('this.userType', this.userType);
        if (this.userType == 3) {
          // 查询劳工
          this.getPersonList()
          return
        }

        console.log('//查询包工头');
        uni.$post(this.path + '/person/query', {
          isHeader: 3, //此参数需动态判断-------------------------------》
          pageNo: 1,
          pageSize: 5000,
          personGroup: 10,
          personIdentity: 3,
          personStatus: 3,
          returnFields: ""
        }).then(res => {
          console.log('<----------');
          if (res.code == 200) {
            console.log('------->', res);
            this.workerary = res.data.records
             this.showWorkerary = res.data.records
            // this.workerary.push(this.mie)
            console.log(this.workerary);
            for (let i = 0; i < this.workerary.length; i++) {
              this.submitlist.push({
                flag: false,
                index: 0,
              })
            }
            console.log(this.submitlist);

          }
        })
        
        
      },
      
      //查询相关人员
      selectework() { 
        let userType = uni.getStorageSync('userType')
        if (userType != 3) {
          // 查询包工头
          this.getLabourList()
          return
        }
        
        uni.$post(this.path + '/person/query', {
          isHeader: 0, //此参数需动态判断-------------------------------》
          pageNo: 1,
          pageSize: 5000,
          personGroup: 10,
          personIdentity: 3,
          personStatus: 3,
          returnFields: ""
        }).then(res => {
          if (res.code == 200) {
            this.workerary = res.data.records
            this.showWorkerary = res.data.records
            console.log(this.workerary);
            for (let i = 0; i < this.workerary.length; i++) {
              this.submitlist.push({
                flag: false,
                index: 0,
              })
            }
            console.log(this.submitlist);

          }
        })
      }
    }
  }
</script>

<style lang="scss">
  .list-box {
    border-radius: 10rpx;
    background-color: white;
    width: 730rpx;
    margin: 0 auto;

    .title {
      width: 710rpx;
      margin: 0 auto;
      margin-top: 5rpx;
      height: 40rpx;
      line-height: 40rpx;
      font-size: 30rpx;
    }

    .list {
      display: flex;
      width: 710rpx;
      margin: 0 auto;
      margin-top: 10rpx;
      justify-content: space-between;
      align-items: center;
      height: 150rpx;

      .left {
        width: 340rpx;
        height: 80rpx;
        display: flex;
        justify-content: space-between;
        align-items: center;

        .img-box {
          width: 80rpx;
          height: 80rpx;

          image {
            width: 100%;
            height: 100%;
          }
        }

        .info-person {
          width: fit-content;
          min-width: 200rpx;
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          height: 80rpx;
          font-size: 13px;

          .zi {
            font-size: 12px;
            color: #c1c1c1;
          }

        }

      }

      .right {
        margin-right: 10rpx;
        width: fit-content;
        display: flex;
        justify-content: space-between;
        align-items: center;
        height: 100rpx;
        font-size: 12px;
        color: #c1c1c1;
      }
    }
  }

  .enter-btn {
    position: fixed;
    bottom: 10px;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;

    button {
      width: 80%;
      background-color: #2f67ff;
      height: 35px;
      line-height: 35px;
      color: white;
      font-size: 14px;
    }
  }
</style>
